package com.three.productList.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.three.core.entity.GoodsImage;
import com.three.core.entity.ProductReview;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.productList.dao.GoodsImageDao;
import com.three.productList.feign.UserMyFeign;
import com.three.productList.service.GoodsImageService;
import com.three.productList.vo.GoodsImageVo;
import com.three.productList.vo.SearchForm;
import jakarta.servlet.http.HttpServletRequest;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author ：li
 * @date ：Created in 2025/3/13 013 18:34
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class GoodsImageServiceImpl implements GoodsImageService {
    @Autowired
    private GoodsImageDao goodsImageDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserMyFeign userMyFeign;

    //查询按钮
    @Override
    public R<IPage<GoodsImage>> selectGoodsImage(SearchForm searchForm) {
        Page<GoodsImage> page = new Page<>(searchForm.getCurrentPage(), searchForm.getPageSize());
        IPage<GoodsImage> goodsImage = goodsImageDao.selectMyList(page, searchForm);
        return new R<IPage<GoodsImage>>(200,"查询成功",goodsImage);

    }
    //删除按钮
    @Override
    public R<GoodsImage> deleteProductImageById(Integer id) {
        int i = goodsImageDao.deleteById(id);
        if (i>0){
            return new R<>(200,"删除成功",null);
        }else {
            return new R<>(500,"删除失败",null);
        }
    }
    //添加按钮
    @Override
    public R<GoodsImage> insertProductImage(GoodsImageVo goodsImageVo) {
        //获取当前用户的信息
        String o = (String) redisTemplate.opsForHash().get("user::login::" + request.getHeader("token"), "name");
        R<User> userR = userMyFeign.selectByName(o);
        //封装数据
        GoodsImage goodsImage = new GoodsImage();
        goodsImage.setCreator(userR.getData().getUserId());
        goodsImage.setProductId(goodsImageVo.getProductId());
        goodsImage.setImageName(goodsImageVo.getImageName());
        goodsImage.setImageurl(goodsImageVo.getImageurl());
        goodsImage.setDescription(goodsImageVo.getDescription());
        goodsImage.setCreatetime(LocalDateTime.now());
        int row = goodsImageDao.insert(goodsImage);
        if (row>0){
            return new R<>(200,"添加成功",null);
        }else {
            return new R<>(500,"添加失败",null);
        }
    }
    //修改按钮
    @Override
    public R<GoodsImage> updateProductImage(GoodsImage goodsImage) {
        int i = goodsImageDao.updateByMyId(goodsImage);
        if (i==0){
            return new R<>(500,"修改失败",null);
        }
        return new R<GoodsImage>(200,"修改成功",null);
    }
}
